import { request } from "@umijs/max";
import { TYPE } from "../pages/types";

type table = {
  tableData: any[];
  total: number;
};

const isPage = (key: string) => key === "current" || key === "pageSize";

export default async function requestTable(path: string, params: any) {
  const query: any = {};
  for (const key in params) {
    if (isPage(key)) continue;

    let value: string = params[key];
    delete params[key];
    if (value === "") continue;

    query[key] = value;
  }
  if (Object.keys(query).length) params.query = query;
  const res: TYPE.Response = await request(path, { params });
  const { tableData, total } = res.data as table;
  return {
    success: res.success,
    data: tableData,
    total,
  };
};
